package com.yw.smartcity_study;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText accountEt;
    private EditText pwdEt;
    private Button loginBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        accountEt = (EditText) findViewById(R.id.accountEt);
        pwdEt = (EditText) findViewById(R.id.pwdEt);
        loginBt = (Button) findViewById(R.id.loginBt);


        //进入登录页的时候   先去sp中拿账号和密码，如果有的话，直接赋值给edittext
        String account = SPUtil.getInstance().getString("sp_account", "");
        String pwd = SPUtil.getInstance().getString("sp_pwd", "");
        accountEt.setText(account);
        pwdEt.setText(pwd);

        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //  "   abc   "
                //拿到账号和密码  做一个非空判断
                String account = accountEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(App.app, "账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(App.app, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }

                //把账号密码保存到sp中
                SPUtil.getInstance().saveString("sp_account", account);
                SPUtil.getInstance().saveString("sp_pwd", pwd);

                //跳转到MainActivity
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }
        });


    }
}